package com.frt.sygang.controller;/**
 * Created by 冯瑞涛 on 2022/9/21 16:54
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.frt.sygang.common.R;
import com.frt.sygang.entity.User;
import com.frt.sygang.sevice.UserService;
import com.frt.sygang.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 冯瑞涛
 * @version 1.0
 * @date 2022/9/21 16:54
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送手机短信验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();

        if(StringUtils.isNotEmpty(phone)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);

            //调用阿里云提供的短信服务API完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //将生成的验证码缓存到Redis中，并且设置有效期为5分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("手机验证码短信发送成功");
        }

        return R.error("短信发送失败");
    }

    @PostMapping("/login")
    public R<User> userLogin(@RequestBody Map map,HttpSession session) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String phone = map.get("phone").toString();
//        String codeTest = (String) valueOperations.get(phone);
        log.info("phone--->"+phone);
//        String code = map.get("code").toString();
//        if(codeTest==null){
//            return R.error("验证码未发送，请重新发送");
//        }
//        if(!code.equals(codeTest)){
//         return R.error("验证码错误，请仔细确认后填写");
//        }
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,phone);
        User user=new User();
        User user1 = userService.getOne(queryWrapper);
        log.info("查询用户是否存在");
        if(user1==null){
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }else {
            user.setId(user1.getId());
        }
        session.setAttribute("user",user.getId());
        if(user1!=null){
//            redisTemplate.delete(phone);
            return R.success(user1);
        }
//        redisTemplate.delete(phone);
        return R.success(user);
    }

    @PostMapping("/loginout")
    public R<String> userLoginOut(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("退出登录");
    }

}
